package study.translation.grammar;

import study.translation.utils.StringUtils;

import java.io.Serializable;
import java.util.List;

public class Rule implements Serializable
{
    private Nonterminal leftPart;

    private List<Symbol> rightPart;

    public Rule( Nonterminal leftPart, List<Symbol> rightPart )
    {
        this.leftPart = leftPart;
        this.rightPart = rightPart;
        leftPart.addRule( this );
    }

    public Nonterminal getLeftPart()
    {
        return leftPart;
    }

    public List<Symbol> getRightPart()
    {
        return rightPart;
    }

    public boolean isEmpty()
    {
        return rightPart.isEmpty();
    }

    public int hashCode()
    {
        return leftPart.hashCode() + rightPart.hashCode();
    }

    public String toString()
    {
        return leftPart + " ::= " + StringUtils.join( rightPart, " " );
    }
}
